package com.example.lenovo.mywechat.login;


import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lenovo.mywechat.R;
import com.example.lenovo.mywechat.homepage.HomepageActivity;
import com.snow.proto.my_wechat.AccountPassword;
import com.snow.proto.my_wechat.FriendInfoList;
import com.snow.proto.my_wechat.MyWeChatServiceGrpc;
import com.snow.proto.my_wechat.UserId;
import com.snow.proto.my_wechat.UserInfo;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;

public class Login extends AppCompatActivity {
    int step;

    String phone_number;
    String password;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.login_by_phone_number);

        step = 1;

        ((FrameLayout)findViewById(R.id.fl_login_two_step)).setVisibility(View.GONE);

        findViewById(R.id.btn_next).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                phone_number = ((EditText)findViewById(R.id.et_phone_number)).getText().toString();

                ((TextView)findViewById(R.id.tv_phone_number)).setText(phone_number);

                ((FrameLayout)findViewById(R.id.fl_login_one_step)).setVisibility(View.GONE);
                ((FrameLayout)findViewById(R.id.fl_login_two_step)).setVisibility(View.VISIBLE);

                if (step == 1){
                    step ++;
                }
            }
        });

        findViewById(R.id.btn_login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                password = ((EditText)findViewById(R.id.et_password)).getText().toString();

//                Toast.makeText(Login.this, phone_number + password, Toast.LENGTH_SHORT).show();
                MyWeChatServiceGrpc.MyWeChatServiceBlockingStub blockingStub = MyWeChatServiceGrpc.newBlockingStub(HomepageActivity.managedChannel);

                UserInfo userInfo = blockingStub.getUserInfoByAccountPassword(
                        AccountPassword.newBuilder().setAccount(phone_number).setPassword(password).build()
                );
                if(userInfo.getId() == -1){
                    Toast.makeText(Login.this, "用户或密码不对！", Toast.LENGTH_SHORT).show();

                    ((FrameLayout)findViewById(R.id.fl_login_one_step)).setVisibility(View.VISIBLE);
                    ((FrameLayout)findViewById(R.id.fl_login_two_step)).setVisibility(View.GONE);
                    step --;
                }else{

                    HomepageActivity.userInfo = userInfo;

                    Intent intent = new Intent(Login.this, HomepageActivity.class);
                    Login.this.startActivity(intent);
                    Login.this.finish();
                }
            }
        });
    }

    @Override
    public void onBackPressed() {
        if (step == 1){
            super.onBackPressed();
        } else if (step == 2) {
            ((FrameLayout)findViewById(R.id.fl_login_one_step)).setVisibility(View.VISIBLE);
            ((FrameLayout)findViewById(R.id.fl_login_two_step)).setVisibility(View.GONE);
            step --;
        } else {
            super.onBackPressed();
        }
    }
}
